<template>
  <div class="app-container">
    <!-- 表单查询 -->
    <el-form :inline="true" class="demo-form-inline">
      <div style="margin-top: 20px">
        <el-form-item>
          <el-input
            v-model="searchObj.coContent"
            :span="8"
            placeholder="评论内容"
          />
        </el-form-item>

        <el-button type="primary" icon="el-icon-search" @click="getList()"
          >查询</el-button
        >
      </div>
      <!-- 工具条 -->
      <div>
        <el-button type="danger" size="mini" @click="removeRows()"
          >批量删除</el-button
        >
      </div>
    </el-form>
    <!-- 复选框 -->
    <el-table
      :data="list"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    />
    <!-- banner列表 -->
    <el-table
      :data="list"
      border
      style="width: 100%; margin-top: 10px"
      @selection-change="handleSelectionChange"
    >
      <!-- 复选框 -->
      <el-table-column type="selection" width="55" />

      <el-table-column type="index" width="50" label="序号" />

      <el-table-column prop="param.uName" label="用户名" />
      <el-table-column prop="coContent" label="评论内容" />
      <el-table-column prop="param.content" label="所在信息" />

      <el-table-column prop="createTime" label="发布时间" />

      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            icon="el-icon-delete"
            @click="removeDataById(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="current"
      :page-size="limit"
      :total="total"
      :page-sizes="[5, 10, 20, 50]"
      style="padding: 30px 0; text-align: center"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @current-change="getList"
      @size-change="changeSize"
    />
  </div>
</template>


<script>
//引入接口定义的js文件
import commentSet from "@/api/commentSet";

export default {
  //定义变量和初始值
  data() {
    return {
      current: 1, //当前页
      limit: 10, //每页显示记录数
      searchObj: {}, //条件封装对象
      list: [], //每页数据集合
      total: 0, //总记录数
      multipleSelection: [], // 批量选择中选择的记录列表
    };
  },
  created() {
    //在页面渲染之前执行
    //一般调用methods定义的方法，得到数据
    this.getList();
  },
  //定义方法，进行请求接口调用
  methods: {
    //获取选择复选框的id值
    handleSelectionChange(selection) {
      this.multipleSelection = selection;
      console.log(selection);
    },

    //用户设置列表
    getList(page = 1) {
      //添加当前页参数
      this.current = page;
      commentSet
        .getList(this.current, this.limit, this.searchObj)
        .then((response) => {
          //请求成功response是接口返回数据
          //返回集合赋值list
          this.list = response.data.records;
          //总记录数
          this.total = response.data.total;
        })
        .catch((error) => {
          //请求失败
          console.log("失败" + error);
        });
    },
    //删除的方法
    removeDataById(id) {
      this.$confirm("是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //确定执行then方法
          //调用接口
          commentSet.delete(id).then((response) => {
            //提示
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            //刷新页面
            this.getList(1);
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //批量删除
    removeRows() {
      if (this.multipleSelection.length < 1) {
        this.$message({
          type: "warning",
          message: "请选择数据!",
        });
        return;
      }

      this.$confirm("是否批量删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //确定执行then方法
        const idList = []
        //遍历数组得到每个id值，设置到idList里面
        for (let i = 0; i < this.multipleSelection.length; i++) {
          const obj = this.multipleSelection[i]
          const id = obj.coid
          idList.push(id);
        }
        //调用接口
        commentSet.removeRows(idList).then((response) => {
          //提示
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          //刷新页面
          this.getList(1);
        });
      });
    },
    // 当页码发生改变的时候
    changeSize(size) {
      this.limit = size;
      this.getList(1);
    },
  },
};
</script>
